﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class DiaDiemDAO
    {
        public static List<DiaDiemDTO> LayDanhSach()
        {
            List<DiaDiemDTO> lstDiaDiem = null;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM DIA_DIEM", cnn);
                lstDiaDiem = new List<DiaDiemDTO>();
                try
                {
                    OleDbDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        DiaDiemDTO diaDiem = new DiaDiemDTO();
                        diaDiem.LoaiDiaDiem = (int)rd["LoaiDiaDiem"];
                        diaDiem.MaDiaDiem = (int)rd["MaDiaDiem"];
                        diaDiem.TenDiaDiem = (string)rd["TenDiaDiem"];
                        if (rd["SoDienThoai"] != System.DBNull.Value)
                            diaDiem.SoDienThoai = (string)rd["SoDienThoai"];
                        else
                            diaDiem.SoDienThoai = "";
                        
                        diaDiem.NguoiLienHe = (string)rd["NguoiLienHe"];
                        if (rd["Email"] == System.DBNull.Value)
                            diaDiem.Email = "";
                        else
                            diaDiem.Email = (string)rd["Email"];
                        diaDiem.DiaChi = (string)rd["DiaChi"];

                        if (rd["ToaDo"] == System.DBNull.Value)
                            diaDiem.ToaDo = "";
                        else
                            diaDiem.ToaDo = (string)rd["ToaDo"];
                        lstDiaDiem.Add(diaDiem);
                    }
                }
                catch (System.Exception ex)
                {
                    if (cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return lstDiaDiem;
        }
        
        public static int Them(DiaDiemDTO diaDiemDto)
        {
            int result = 0;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                string strFormat = "Insert into Dia_Diem(MaDiaDiem, TenDiaDiem, LoaiDiaDiem, DiaChi, SoDienThoai, Email, ToaDo, NguoiLienhe)";
                strFormat += "Values(?, ?, ?, ?, ?, ?, ?, ?)";
                OleDbCommand cmd = new OleDbCommand(strFormat, cnn);

                cmd.Parameters.Add("@MaDiaDiem", OleDbType.Integer);
                cmd.Parameters.Add("@TenDiaDiem", OleDbType.VarWChar);
                cmd.Parameters.Add("@LoaiDiaDiem", OleDbType.Integer);
                cmd.Parameters.Add("@DiaChi", OleDbType.VarWChar);
                cmd.Parameters.Add("@SoDienThoai", OleDbType.VarChar);
                cmd.Parameters.Add("@Email", OleDbType.VarChar);
                cmd.Parameters.Add("@ToaDo", OleDbType.VarChar);
                cmd.Parameters.Add("@NguoiLienhe", OleDbType.VarWChar);

                cmd.Parameters["@MaDiaDiem"].Value = diaDiemDto.MaDiaDiem;
                cmd.Parameters["@TenDiaDiem"].Value = diaDiemDto.TenDiaDiem;
                cmd.Parameters["@LoaiDiaDiem"].Value = diaDiemDto.LoaiDiaDiem;
                cmd.Parameters["@DiaChi"].Value = diaDiemDto.DiaChi;
                cmd.Parameters["@SoDienThoai"].Value = diaDiemDto.SoDienThoai;
                cmd.Parameters["@Email"].Value = diaDiemDto.Email;
                cmd.Parameters["@ToaDo"].Value = diaDiemDto.ToaDo;
                cmd.Parameters["@NguoiLienhe"].Value = diaDiemDto.NguoiLienHe;

                try
                {
                    result = cmd.ExecuteNonQuery();
                    cnn.Close();
                }
                catch (System.Exception ex)
                {
                	if(cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }

    }
}
